import hyRequest from '@/service'
import { PagesKey } from '@/global/constant'

// 分页数据
export const getPageList = (pageKey: PagesKey, params: any) => {
  // 菜单页没有分页也没有搜索的参数，所以params置空
  if (pageKey === PagesKey.MenuList) {
    params = {}
  }
  return hyRequest.get({
    url: `/${pageKey}/getList`,
    params
  })
}

// 删除
export const reqPageDelete = (pageKey: PagesKey, id = -1) => {
  // url默认值必须为null或者undefined，不能为'',因为空字符在 a ?? b会返回a
  let url = null
  // 这两个页面公用一个删除接口，单独判断
  if (pageKey === PagesKey.LoginLog || pageKey === PagesKey.OperationLog) {
    url = `/log/${id}`
  } else if (
    pageKey === PagesKey.IntegralConsume ||
    pageKey === PagesKey.IntegralObtain
  ) {
    url = `/integral/detail/${id}`
  } else {
    url = `/${pageKey}/${id}`
  }
  return hyRequest.delete({
    url,
    isShowMessage: true
  })
}

// 新增
export const reqPageAdd = (pageKey: PagesKey, data: any) => {
  return hyRequest.post({
    url: `/${pageKey}`,
    data: { ...data },
    isShowMessage: true
  })
}

// 批量删除
export const reqPageBatchDelete = (pageKey: PagesKey, encryptStr: string) => {
  return hyRequest.post({
    url: `/${pageKey}/batchDelete`,
    data: { encryptStr },
    isShowMessage: true
  })
}

// 编辑
export const reqPageEdit = (pageKey: PagesKey, data: any) => {
  return hyRequest.put({
    url: `/${pageKey}`,
    data: { ...data },
    isShowMessage: true
  })
}
